package me.zhengjie.modules.system.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.system.domain.Department;
import me.zhengjie.modules.system.service.DepartmentService;
import me.zhengjie.modules.system.service.dto.DepartmentDTO;
import me.zhengjie.modules.system.service.dto.DepartmentQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* @author Zhang xing wang
* @date 2020-03-26
*/
@Api(tags = "Department管理")
@RestController
@RequestMapping("/api/department")
public class DepartmentController {

    private final DepartmentService departmentService;

    public DepartmentController(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('department:list')")
    public void download(HttpServletResponse response, DepartmentQueryCriteria criteria) throws IOException {
        departmentService.download(departmentService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询Department")
    @ApiOperation("查询Department")
    @PreAuthorize("@el.check('department:list')")
    public ResponseEntity getDepartments(DepartmentQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(departmentService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping("/tree")
    @Log("查询TreeDepartment")
    @ApiOperation("查询TreeDepartment")
    public ResponseEntity tree(@RequestBody Department resources){
        DepartmentQueryCriteria criteria = new DepartmentQueryCriteria();
        criteria.setName(resources.getName());
        List<DepartmentDTO> list = departmentService.queryAll(criteria);
        Map<String, Object> map = departmentService.terrDepartment(list);
        return new ResponseEntity<>(map,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增Department")
    @ApiOperation("新增Department")
    @PreAuthorize("@el.check('department:add')")
    public ResponseEntity create(@Validated @RequestBody Department resources){
        return new ResponseEntity<>(departmentService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改Department")
    @ApiOperation("修改Department")
    @PreAuthorize("@el.check('department:edit')")
    public ResponseEntity update(@Validated @RequestBody Department resources){
        departmentService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping(value = "/{id}")
    @Log("删除Department")
    @ApiOperation("删除Department")
    @PreAuthorize("@el.check('department:del')")
    public ResponseEntity delete(@PathVariable String id){
        departmentService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}